package com.zz.hhbl.ui

import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.os.Environment
import android.util.Log
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.zz.framework.core.base.BaseFragment
import com.zz.framework.core.ext.onClick
import com.zz.framework.core.ext.viewBinding
import com.zz.framework.core.toast.ToastUtils
import com.zz.framework.core.utils.LogUtils
import com.zz.hhbl.R
import com.zz.hhbl.databinding.FragmentTestBinding
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Date
import kotlin.jvm.java

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/25 10:41.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
@AndroidEntryPoint
class TestFragment : BaseFragment(R.layout.fragment_test) {
    val binding by viewBinding<FragmentTestBinding>()
    override fun initView() {
//        Aria.download(this).register()
        val mUrls = listOf(
            "https://appdownload.alicdn.com/publish/cainiao4android/latest/cainiao4android_1598322688221.apk",
            "https://yanxuan-dl.nosdn.127.net/yan/android/yanxuan-aos_out_3547.apk"
        )
        val names = listOf(
            "aaa.apk",
            "bbb.apk"
        )
        binding?.apply {
            XXPermissions.with(requireActivity())
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .request { permissions, allGranted -> {} }
//            val entity: DownloadGroupEntity? = Aria.download(this).getGroupEntity(mUrls)
            button1.onClick {
//                val mTaskId = Aria.download(this@TestFragment)
//                    .loadGroup(mUrls)
//                    .setDirPath(
//                        Environment.getExternalStorageDirectory().path + "/Download/hhbl"
//                    )
//                    .setSubFileName(names)
//                    .unknownSize()
//                    .option(getHttpOption())
//                    .ignoreFilePathOccupy() //.setFileSize(114981416)
//                    .create()
            }
            button2.onClick {
                location()
            }
            button3.onClick {
            }
            button4.onClick {
            }
        }
    }

//    private fun getHttpOption(): HttpOption {
//        val option = HttpOption()
//        option.setFileLenAdapter(HttpFileLenAdapter())
//        return option
//    }
//
//    val TAG = "TAG"
//    @DownloadGroup.onWait
//    fun taskWait(task: DownloadGroupTask) {
//        ALog.d(TAG, task.taskName + "wait")
//    }
//
//    @DownloadGroup.onPre
//    protected fun onPre(task: DownloadGroupTask) {
//        ALog.d(TAG, "group pre, p = " + task.percent)
//    }
//
//    @DownloadGroup.onTaskPre
//    protected fun onTaskPre(task: DownloadGroupTask) {
//        ALog.d(TAG, "group task pre, p = " + task.percent)
//    }
//
//    @DownloadGroup.onTaskStart
//    fun taskStart(task: DownloadGroupTask) {
//        ALog.d(TAG, "group task create, p = " + task.percent)
//    }
//
//    @DownloadGroup.onTaskRunning
//    protected fun running(task: DownloadGroupTask) {
//        ALog.d(
//            TAG, ("group running, p = "
//                    + task.percent
//                    + ", speed = "
//                    + task.convertSpeed
//                    + ", current_p = "
//                    + task.currentProgress)
//        )
//    }
//
//    @DownloadGroup.onTaskResume
//    fun taskResume(task: DownloadGroupTask) {
//        ALog.d(TAG, "group task resume, p = " + task.percent)
//    }
//
//    @DownloadGroup.onTaskStop
//    fun taskStop(task: DownloadGroupTask) {
//        ALog.d(TAG, "group task stop")
//    }
//
//    @DownloadGroup.onTaskCancel
//    fun taskCancel(task: DownloadGroupTask) {
//        ALog.d(TAG, "group task cancel")
//    }
//
//    @DownloadGroup.onTaskFail
//    fun taskFail(task: DownloadGroupTask?) {
//        if (task != null) {
//            ALog.d(TAG, "group task fail")
//        }
//    }
//
//    @DownloadGroup.onTaskComplete
//    fun taskComplete(task: DownloadGroupTask) {
//        ALog.d(TAG, "任务组下载完成")
//    }

    //声明mlocationClient对象
    var mlocationClient: AMapLocationClient? = null;

    //声明mLocationOption对象
    var mLocationOption: AMapLocationClientOption? = null;

    fun location() {
        AMapLocationClient.updatePrivacyShow(requireContext(), true, true)
        AMapLocationClient.updatePrivacyAgree(requireContext(), true)
        mlocationClient = AMapLocationClient(requireContext());
        //初始化定位参数
        mLocationOption = AMapLocationClientOption();
        //设置定位监听
        mlocationClient?.setLocationListener(object : AMapLocationListener {
            override fun onLocationChanged(amapLocation: AMapLocation?) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
                        //定位成功回调信息，设置相关消息
                        amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                        amapLocation.getLatitude();//获取纬度
                        amapLocation.getLongitude();//获取经度
                        amapLocation.getAccuracy();//获取精度信息
                        var df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        val date = Date(amapLocation.getTime());
                        df.format(date);//定位时间
                        ToastUtils.show("locationType:${amapLocation.locationType} Latitude:${amapLocation.latitude}  Longitude${amapLocation.longitude}  address:${amapLocation.address}")
                        LogUtils.error("locationType:${amapLocation.locationType} Latitude:${amapLocation.latitude}  Longitude${amapLocation.longitude}  address:${amapLocation.address}")
                    } else {
                        //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                        Log.e(
                            "AmapError", "location Error, ErrCode:"
                                    + amapLocation.getErrorCode() + ", errInfo:"
                                    + amapLocation.getErrorInfo()
                        );
                    }
                }
            }
        });
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption?.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption?.setInterval(2000);
//设置定位参数
        mlocationClient?.setLocationOption(mLocationOption);
        mLocationOption?.isNeedAddress = true
// 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
// 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后，在合适的生命周期调用onDestroy()方法
// 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
//启动定位
        mlocationClient?.startLocation();
    }
}


